package com.zxl.grocery.modular.schedule.controller;

import com.zxl.grocery.common.controller.BaseController;
import com.zxl.grocery.common.exception.BizExceptionEnum;
import com.zxl.grocery.common.exception.BussinessException;
import com.zxl.grocery.common.persistence.dao.ScheduleJobMapper;
import com.zxl.grocery.common.persistence.model.ScheduleJobEntity;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.util.Query;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.modular.schedule.service.IScheduleJobMgrService;
import com.zxl.grocery.modular.schedule.warpper.ScheduleJobMgrWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 定时任务控制器
 *
 * @author zxl
 * @Date 2017-08-23 14:20:31
 */
@Controller
@RequestMapping("/scheduleJobMgr")
public class ScheduleJobMgrController extends BaseController {

    private String PREFIX = "/schedule/scheduleJobMgr/";

    @Resource
    private IScheduleJobMgrService iScheduleJobMgrService;

    @Resource
    private ScheduleJobMapper scheduleJobMapper;

    /**
     * 跳转到定时任务首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "scheduleJobMgr.html";
    }

    /**
     * 跳转到添加定时任务
     */
    @RequestMapping("/scheduleJobMgr_add")
    public String scheduleJobMgrAdd() {
        return PREFIX + "scheduleJobMgr_add.html";
    }

    /**
     * 跳转到修改定时任务
     */
    @RequestMapping("/scheduleJobMgr_update/{scheduleJobMgrId}")
    public String scheduleJobMgrUpdate(@PathVariable Integer scheduleJobMgrId, Model model) {
        if (ToolUtil.isEmpty(scheduleJobMgrId)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        ScheduleJobEntity schelduleJob = scheduleJobMapper.selectById(scheduleJobMgrId);
        System.out.println(schelduleJob.toString());
        model.addAttribute("schelduleJob",schelduleJob);
        LogObjectHolder.me().set(schelduleJob);
        return PREFIX + "scheduleJobMgr_edit.html";
    }

    /**
     * 获取定时任务列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        //查询列表数据
//        Query query = new Query(params);
//        List<ScheduleJobEntity> jobList = iScheduleJobMgrService.queryList(query);
//        int total = iScheduleJobMgrService.queryTotal(query);
        List<Map<String,Object>> result = iScheduleJobMgrService.getAllScheduleJobList();
        return super.warpObject(new ScheduleJobMgrWarpper(result));
    }

    /**
     * 新增定时任务
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ScheduleJobEntity scheduleJob) {
        iScheduleJobMgrService.save(scheduleJob);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除定时任务
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "ids[]") Long[] ids) {//request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话，直接用springMVC的@RequestBody标签接收后面跟实体对象就行了，spring会帮你自动拼装成对象，如果Content-type设置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的东西了，只能以常规的方式获取json串了http://blog.csdn.net/mingtianhaiyouwo/article/details/51459764
        iScheduleJobMgrService.deleteBatch(ids);
        return SUCCESS_TIP;
    }


    /**
     * 修改定时任务
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ScheduleJobEntity scheduleJob) {
        iScheduleJobMgrService.update(scheduleJob);
        return super.SUCCESS_TIP;
    }

    /**
     * 定时任务详情
     */
    @RequestMapping(value = "/detail/{jobId}")
    @ResponseBody
    public Object detail(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = iScheduleJobMgrService.queryObject(jobId);
        return null;
    }
    /**
     * 立即执行任务
     */
    @RequestMapping("/run")
    @ResponseBody
    public Object run(@RequestParam(value = "jobIds[]") Long[] jobIds){
        if(ToolUtil.isEmpty(jobIds)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        iScheduleJobMgrService.run(jobIds);

        return super.SUCCESS_TIP;
    }

    /**
     * 暂停定时任务
     */
    @RequestMapping("/pause")
    @ResponseBody
    public Object pause(@RequestParam(value = "jobIds[]") Long[] jobIds){
        if(ToolUtil.isEmpty(jobIds)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        iScheduleJobMgrService.pause(jobIds);
        System.out.println("haha");
        return super.SUCCESS_TIP;
    }

    /**
     * 恢复定时任务
     */
    @RequestMapping("/resume")
    @ResponseBody
    public Object resume(@RequestParam(value = "jobIds[]") Long[] jobIds){
        if(ToolUtil.isEmpty(jobIds)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        iScheduleJobMgrService.resume(jobIds);

        return super.SUCCESS_TIP;
    }
}
